﻿
Imports System.IO
Imports storoomc
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class put
    Inherits controlProcessor

    Public Const _pathidentity As String = "/p"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Private Shared Sub failOpenTempfile(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                        ByVal tempfilename As String)

    End Sub

    Private Shared Sub failWriteStoroom(ByVal context As httpContext, ByRef control As xmlcontrol, _
                                        ByVal key As String)

    End Sub

    Private Shared Sub nofileFD(ByVal context As httpContext, ByRef control As xmlcontrol)

    End Sub

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        assert(Not context Is Nothing, "context is nothing.")

        Dim renameFD As formdata = Nothing
        Dim fileFD As formdata = Nothing

        renameFD = context.formdata(query.rename)
        fileFD = context.formdata(query.file)

        If fileFD Is Nothing Then
            nofileFD(context, control)
        Else
            Dim originalFileName As String = Nothing
            Dim key As String = Nothing
            Dim rename As String = Nothing
            If Not renameFD Is Nothing Then
                copy(rename, renameFD.content())
            End If
            While Not validator.validate(rename, fileFD.filename(), originalFileName, key)
                rename = generateTempFilename(originalFileName)
            End While

            Dim fs As FileStream = Nothing
            Try
                fs = New FileStream(fileFD.tempfilename(), FileMode.Open, FileAccess.Read, FileShare.Read)
            Catch ex As Exception
                raiseError("cannot open file " + fileFD.tempfilename() + ", ex " + ex.Message, _
                           errorHandle.errorType.warning)
            End Try

            If fs Is Nothing Then
                failOpenTempfile(context, control, fileFD.tempfilename())
            Else
                If serialstore.modify(key, fs) Then

                Else
                    failWriteStoroom(context, control, key)
                End If
                closestream(fs)
            End If
        End If
    End Function
End Class
